using System;
using System.Collections;
using System.Security.Cryptography;
using System.Text;
using System.Xml;
namespace Tencent
{
	public class WXBizMsgCrypt
	{
		private enum WXBizMsgCryptErrorCode
		{
			WXBizMsgCrypt_OK,
			WXBizMsgCrypt_ValidateSignature_Error = -40001,
			WXBizMsgCrypt_ParseXml_Error = -40002,
			WXBizMsgCrypt_ComputeSignature_Error = -40003,
			WXBizMsgCrypt_IllegalAesKey = -40004,
			WXBizMsgCrypt_ValidateCorpid_Error = -40005,
			WXBizMsgCrypt_EncryptAES_Error = -40006,
			WXBizMsgCrypt_DecryptAES_Error = -40007,
			WXBizMsgCrypt_IllegalBuffer = -40008,
			WXBizMsgCrypt_EncodeBase64_Error = -40009,
			WXBizMsgCrypt_DecodeBase64_Error = -40010
		}
		public class DictionarySort : IComparer
		{
			public int Compare(object oLeft, object oRight)
			{
				string text = oLeft as string;
				string text2 = oRight as string;
				int length = text.Length;
				int length2 = text2.Length;
				int num = 0;
				while (num < length && num < length2)
				{
					if (text[num] < text2[num])
					{
						return -1;
					}
					if (text[num] > text2[num])
					{
						return 1;
					}
					num++;
				}
				return length - length2;
			}
		}
		private string m_sToken;
		private string m_sEncodingAESKey;
		private string m_sCorpID;
		public WXBizMsgCrypt(string sToken, string sEncodingAESKey, string sCorpID)
		{
			this.m_sToken = sToken;
			this.m_sCorpID = sCorpID;
			this.m_sEncodingAESKey = sEncodingAESKey;
		}
		public int VerifyURL(string sMsgSignature, string sTimeStamp, string sNonce, string sEchoStr, ref string sReplyEchoStr)
		{
			if (this.m_sEncodingAESKey.Length != 43)
			{
				return -40004;
			}
			int num = WXBizMsgCrypt.VerifySignature(this.m_sToken, sTimeStamp, sNonce, sEchoStr, sMsgSignature);
			if (num != 0)
			{
				return num;
			}
			sReplyEchoStr = "";
			string a = "";
			try
			{
				sReplyEchoStr = Cryptography.AES_decrypt(sEchoStr, this.m_sEncodingAESKey, ref a);
			}
			catch (Exception)
			{
				sReplyEchoStr = "";
				int result = -40007;
				return result;
			}
			if (a != this.m_sCorpID)
			{
				sReplyEchoStr = "";
				return -40005;
			}
			return 0;
		}
		public int DecryptMsg(string sMsgSignature, string sTimeStamp, string sNonce, string sPostData, ref string sMsg)
		{
			if (this.m_sEncodingAESKey.Length != 43)
			{
				return -40004;
			}
			XmlDocument xmlDocument = new XmlDocument();
			string innerText;
			try
			{
				xmlDocument.LoadXml(sPostData);
				XmlNode firstChild = xmlDocument.FirstChild;
				innerText = firstChild["Encrypt"].InnerText;
			}
			catch (Exception)
			{
				int result = -40002;
				return result;
			}
			int num = WXBizMsgCrypt.VerifySignature(this.m_sToken, sTimeStamp, sNonce, innerText, sMsgSignature);
			if (num != 0)
			{
				return num;
			}
			string a = "";
			try
			{
				sMsg = Cryptography.AES_decrypt(innerText, this.m_sEncodingAESKey, ref a);
			}
			catch (FormatException)
			{
				sMsg = "";
				int result = -40010;
				return result;
			}
			catch (Exception)
			{
				sMsg = "";
				int result = -40007;
				return result;
			}
			if (a != this.m_sCorpID)
			{
				return -40005;
			}
			return 0;
		}
		public int EncryptMsg(string sReplyMsg, string sTimeStamp, string sNonce, ref string sEncryptMsg)
		{
			if (this.m_sEncodingAESKey.Length != 43)
			{
				return -40004;
			}
			string text = "";
			try
			{
				text = Cryptography.AES_encrypt(sReplyMsg, this.m_sEncodingAESKey, this.m_sCorpID);
			}
			catch (Exception)
			{
				int result = -40006;
				return result;
			}
			string str = "";
			int num = WXBizMsgCrypt.GenarateSinature(this.m_sToken, sTimeStamp, sNonce, text, ref str);
			if (num != 0)
			{
				return num;
			}
			sEncryptMsg = "";
			string text2 = "<Encrypt><![CDATA[";
			string text3 = "]]></Encrypt>";
			string str2 = "<MsgSignature><![CDATA[";
			string str3 = "]]></MsgSignature>";
			string str4 = "<TimeStamp><![CDATA[";
			string str5 = "]]></TimeStamp>";
			string str6 = "<Nonce><![CDATA[";
			string str7 = "]]></Nonce>";
			sEncryptMsg = string.Concat(new string[]
			{
				sEncryptMsg,
				"<xml>",
				text2,
				text,
				text3
			});
			sEncryptMsg = sEncryptMsg + str2 + str + str3;
			sEncryptMsg = sEncryptMsg + str4 + sTimeStamp + str5;
			sEncryptMsg = sEncryptMsg + str6 + sNonce + str7;
			sEncryptMsg += "</xml>";
			return 0;
		}
		private static int VerifySignature(string sToken, string sTimeStamp, string sNonce, string sMsgEncrypt, string sSigture)
		{
			string a = "";
			int num = WXBizMsgCrypt.GenarateSinature(sToken, sTimeStamp, sNonce, sMsgEncrypt, ref a);
			if (num != 0)
			{
				return num;
			}
			if (a == sSigture)
			{
				return 0;
			}
			return -40001;
		}
		public static int GenarateSinature(string sToken, string sTimeStamp, string sNonce, string sMsgEncrypt, ref string sMsgSignature)
		{
			ArrayList arrayList = new ArrayList();
			arrayList.Add(sToken);
			arrayList.Add(sTimeStamp);
			arrayList.Add(sNonce);
			arrayList.Add(sMsgEncrypt);
			arrayList.Sort(new WXBizMsgCrypt.DictionarySort());
			string text = "";
			for (int i = 0; i < arrayList.Count; i++)
			{
				text += arrayList[i];
			}
			string text2 = "";
			try
			{
				SHA1 sHA = new SHA1CryptoServiceProvider();
				ASCIIEncoding aSCIIEncoding = new ASCIIEncoding();
				byte[] bytes = aSCIIEncoding.GetBytes(text);
				byte[] value = sHA.ComputeHash(bytes);
				text2 = BitConverter.ToString(value).Replace("-", "");
				text2 = text2.ToLower();
			}
			catch (Exception)
			{
				return -40003;
			}
			sMsgSignature = text2;
			return 0;
		}
	}
}
